#include "part1_shared.h"

int read_input(char *input) {

	char c;
	int i = 0;
	int long_input = 0;
	input[0] = '\0';

	input[0] = '\0';
	while (1) {
		c = getchar();
		if (c == '\n') {
			input[i] = '\0';
			break;
		}
		if (i > MAX_INTPUT_SIZE) {
			long_input = 1;
			
		}
		input[i] = c;
		i++;
	}

	if (long_input) {
		fprintf(stderr,"Input was to long, please try a shorter one\n");
		return -1;
	}
	return 1;
}

int extract_username_and_passowrd(char *user_input, char *username, char *password) {
	
	char c;
	int i = 0 , k = 0;

	while ( (c = user_input[k]) != '\t') {
		if ((c == ' ') || (c == '\0')) return 0;
		username[i] = c;
		i++;
		k++;
	}
	username[i] = '\0';
	if (i == 0) return 0;
	i = 0;
	k++;
	while ( (c = user_input[k]) != '\0') {
		if ((c == '\t') || (c == ' ')) return 0;
		password[i] = c;
		i++;
		k++;	
	}
	if (i == 0) return 0;
	password[i] = '\0';
	return 1;
}

void hash_password(BasicHashFunctionPtr hash, char *password, char *hased_password) {
	unsigned char buffer[MAX_HASH_SIZE];
	int hash_size;
	if (hash == MD5BasicHash) hash_size = MD5_OUTPUT_LENGTH_IN_BYTES;
	if (hash == SHA1BasicHash) hash_size = SHA1_OUTPUT_LENGTH_IN_BYTES;
	cryptHash(hash, password, buffer);
	binary2hexa(buffer, hash_size, hased_password,MAX_INTPUT_SIZE);
}

int string_length(char *string) {
	int i = 0;
	while (string[i] != '\0') i++;
	return i + 1;
}